using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;
using System.Xml;
using Newtonsoft.Json.Linq;

namespace BUS
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class ChuyenLCDControl : BUSControl
    {
        #region Local Variable
        private ChuyenLCDDataAccess _objDAO;
        #endregion Local Variable

        public ChuyenLCDControl(string type, string connectString, int timeout = 0)
            : base(type, connectString, timeout) { _objDAO = new ChuyenLCDDataAccess(Connnection); }

        public ChuyenLCDControl(BUSControl common)
            : base(common) { _objDAO = new ChuyenLCDDataAccess(common.Connnection); }

        #region Method
        public DataTable GetShortData(String XuongId, List<DTO.FilterParameter> filters, ref string sErr)
        {
            return _objDAO.GetShortData(XuongId, filters, ref sErr);
        }

        public DataTable GetByFilterToDataTable(String XuongId, List<DTO.FilterParameter> filters, ref string logMsg, int indexPage = 0, int itemPerPage = 0)
        {
            return _objDAO.GetByFilterToDataTable(XuongId, filters, ref logMsg, indexPage, itemPerPage);
        }

        public int GetCountRecord(String XuongId, List<DTO.FilterParameter> filters, ref string sErr)
        {
            return _objDAO.GetCountRecord(XuongId, filters, ref sErr);
        }

        public ChuyenLCDInfo[] GetByFilter(String XuongId, List<DTO.FilterParameter> filters, ref string logMsg, int indexPage = 0, int itemPerPage = 0)
        {
            ChuyenLCDInfo[] res = _objDAO.GetByFilter(XuongId, filters, ref logMsg, indexPage, itemPerPage);
            //get details

            return res;
        }

        public int Add(ChuyenLCDInfo obj, ref string sErr)
        {
            _objDAO.Add(obj, ref sErr);
            if (sErr == "")
            {
                //add details
            }
            return 1;
        }

        public string Update(ChuyenLCDInfo obj)
        {
            string sErr = _objDAO.Update(obj);
            // delete details
            // add details
            return sErr;
        }

        public string Delete(String XuongId, Int32 LineId)
        {
            string sErr = "";
            // delete details

            if (sErr == "") sErr = _objDAO.Delete(XuongId, LineId);
            return sErr;
        }
        public bool IsExist(String XuongId, Int32? LineId, ref string sErr)
        {
            return _objDAO.IsExist(XuongId, LineId, ref sErr);
        }

        public string InsertUpdate(ChuyenLCDInfo obj)
        {
            string sErr = "";
            if (IsExist(obj.XuongId, obj.LineId, ref sErr)) { sErr = Update(obj); }
            else
            {
                if (sErr == "") Add(obj, ref sErr);
            }
            return sErr;
        }

        public string TransferIn(DataRow row)
        {
            ChuyenLCDInfo inf = new ChuyenLCDInfo(row);
            return InsertUpdate(inf);
        }

        public override string ToXmlString(DTOInfo[] lInf, ref string sErr)
        {
            string result = "";
            try
            {
                if (lInf != null && lInf.Length > 0)
                {
                    XmlDocument doc = new XmlDocument();
                    XmlElement root = doc.CreateElement("ChuyenLCD");
                    doc.AppendChild(root);
                    foreach (DTO.DTOInfo x in lInf)
                    {
                        DTO.ChuyenLCDInfo it = x as DTO.ChuyenLCDInfo;
                        if (it == null) return "";

                        XmlElement row = doc.CreateElement("row");

                        if (it.LineId != null) row.SetAttribute("LineId", string.Format("{0}", it.LineId));
                        if (it.CongNhan1 != null) row.SetAttribute("CongNhan1", string.Format("{0}", it.CongNhan1));
                        if (it.CongNhan2 != null) row.SetAttribute("CongNhan2", string.Format("{0}", it.CongNhan2));
                        if (it.CongNhan3 != null) row.SetAttribute("CongNhan3", string.Format("{0}", it.CongNhan3));
                        if (it.CongNhan4 != null) row.SetAttribute("CongNhan4", string.Format("{0}", it.CongNhan4));
                        if (it.CongNhan5 != null) row.SetAttribute("CongNhan5", string.Format("{0}", it.CongNhan5));
                        if (it.CongNhan6 != null) row.SetAttribute("CongNhan6", string.Format("{0}", it.CongNhan6));
                        if (it.CongNhan7 != null) row.SetAttribute("CongNhan7", string.Format("{0}", it.CongNhan7));
                        if (it.SanLuong1 != null) row.SetAttribute("SanLuong1", string.Format("{0}", it.SanLuong1));
                        if (it.SanLuong2 != null) row.SetAttribute("SanLuong2", string.Format("{0}", it.SanLuong2));
                        if (it.SanLuong3 != null) row.SetAttribute("SanLuong3", string.Format("{0}", it.SanLuong3));
                        if (it.SanLuong4 != null) row.SetAttribute("SanLuong4", string.Format("{0}", it.SanLuong4));
                        if (it.SanLuong5 != null) row.SetAttribute("SanLuong5", string.Format("{0}", it.SanLuong5));
                        if (it.SanLuong6 != null) row.SetAttribute("SanLuong6", string.Format("{0}", it.SanLuong6));
                        if (it.SanLuong7 != null) row.SetAttribute("SanLuong7", string.Format("{0}", it.SanLuong7));
                        if (it.SanLuong8 != null) row.SetAttribute("SanLuong8", string.Format("{0}", it.SanLuong8));
                        if (it.SanLuong9 != null) row.SetAttribute("SanLuong9", string.Format("{0}", it.SanLuong9));
                        if (it.SanLuong10 != null) row.SetAttribute("SanLuong10", string.Format("{0}", it.SanLuong10));
                        if (it.SanLuong11 != null) row.SetAttribute("SanLuong11", string.Format("{0}", it.SanLuong11));
                        if (it.SanLuong12 != null) row.SetAttribute("SanLuong12", string.Format("{0}", it.SanLuong12));
                        if (it.SanLuong13 != null) row.SetAttribute("SanLuong13", string.Format("{0}", it.SanLuong13));
                        if (it.SanLuong14 != null) row.SetAttribute("SanLuong14", string.Format("{0}", it.SanLuong14));
                        if (it.SanLuong15 != null) row.SetAttribute("SanLuong15", string.Format("{0}", it.SanLuong15));
                        if (it.SanLuong16 != null) row.SetAttribute("SanLuong16", string.Format("{0}", it.SanLuong16));
                        if (it.SanLuong17 != null) row.SetAttribute("SanLuong17", string.Format("{0}", it.SanLuong17));
                        if (it.SanLuong18 != null) row.SetAttribute("SanLuong18", string.Format("{0}", it.SanLuong18));
                        if (it.SanLuong19 != null) row.SetAttribute("SanLuong19", string.Format("{0}", it.SanLuong19));
                        if (it.SanLuong20 != null) row.SetAttribute("SanLuong20", string.Format("{0}", it.SanLuong20));
                        // details
                        //if (it.details != null) row.SetAttribute("details", JsonConvert.SerializeObject(it.details));

                        root.AppendChild(row);
                    }

                    result = doc.InnerXml;
                }
            }
            catch (Exception ex) { sErr = ex.Message; }
            return result;
        }

        public override string TransferIn(DTOInfo inf, BUS.BUSControl.TransferMode mode)
        {
            string sErr = "";
            if (inf is ChuyenLCDInfo)
            {
                if (mode == BUS.BUSControl.TransferMode.AddNew) Add((DTO.ChuyenLCDInfo)inf, ref sErr);
                else if (mode == BUS.BUSControl.TransferMode.Update) sErr = Update((ChuyenLCDInfo)inf);
                else sErr = InsertUpdate((ChuyenLCDInfo)inf);
            }
            return sErr;
        }

        public override DTO.DTOInfo[] Preview(XmlDocument xml, ref string sErr, string db = null)
        {
            DTO.DTOInfo[] lRes = null;
            XmlElement doc = xml.DocumentElement;
            lRes = new DTOInfo[doc.ChildNodes.Count];

            for (int i = 0; i < doc.ChildNodes.Count; i++)
            {
                if (sErr != "") return lRes;
                try
                {
                    XmlElement ele = doc.ChildNodes[i] as XmlElement;

                    DTO.ChuyenLCDInfo inf = new DTO.ChuyenLCDInfo();

                    inf.XuongId = db;
                    if (GetStringAttribute(ele, "LineId") != "") inf.LineId = GetInt32Attribute(ele, "LineId");
                    if (GetStringAttribute(ele, "CongNhan1") != "") inf.CongNhan1 = GetStringAttribute(ele, "CongNhan1");
                    if (GetStringAttribute(ele, "CongNhan2") != "") inf.CongNhan2 = GetStringAttribute(ele, "CongNhan2");
                    if (GetStringAttribute(ele, "CongNhan3") != "") inf.CongNhan3 = GetStringAttribute(ele, "CongNhan3");
                    if (GetStringAttribute(ele, "CongNhan4") != "") inf.CongNhan4 = GetStringAttribute(ele, "CongNhan4");
                    if (GetStringAttribute(ele, "CongNhan5") != "") inf.CongNhan5 = GetStringAttribute(ele, "CongNhan5");
                    if (GetStringAttribute(ele, "CongNhan6") != "") inf.CongNhan6 = GetStringAttribute(ele, "CongNhan6");
                    if (GetStringAttribute(ele, "CongNhan7") != "") inf.CongNhan7 = GetStringAttribute(ele, "CongNhan7");
                    if (GetStringAttribute(ele, "SanLuong1") != "") inf.SanLuong1 = GetInt32Attribute(ele, "SanLuong1");
                    if (GetStringAttribute(ele, "SanLuong2") != "") inf.SanLuong2 = GetInt32Attribute(ele, "SanLuong2");
                    if (GetStringAttribute(ele, "SanLuong3") != "") inf.SanLuong3 = GetInt32Attribute(ele, "SanLuong3");
                    if (GetStringAttribute(ele, "SanLuong4") != "") inf.SanLuong4 = GetInt32Attribute(ele, "SanLuong4");
                    if (GetStringAttribute(ele, "SanLuong5") != "") inf.SanLuong5 = GetInt32Attribute(ele, "SanLuong5");
                    if (GetStringAttribute(ele, "SanLuong6") != "") inf.SanLuong6 = GetInt32Attribute(ele, "SanLuong6");
                    if (GetStringAttribute(ele, "SanLuong7") != "") inf.SanLuong7 = GetInt32Attribute(ele, "SanLuong7");
                    if (GetStringAttribute(ele, "SanLuong8") != "") inf.SanLuong8 = GetInt32Attribute(ele, "SanLuong8");
                    if (GetStringAttribute(ele, "SanLuong9") != "") inf.SanLuong9 = GetInt32Attribute(ele, "SanLuong9");
                    if (GetStringAttribute(ele, "SanLuong10") != "") inf.SanLuong10 = GetInt32Attribute(ele, "SanLuong10");
                    if (GetStringAttribute(ele, "SanLuong11") != "") inf.SanLuong11 = GetInt32Attribute(ele, "SanLuong11");
                    if (GetStringAttribute(ele, "SanLuong12") != "") inf.SanLuong12 = GetInt32Attribute(ele, "SanLuong12");
                    if (GetStringAttribute(ele, "SanLuong13") != "") inf.SanLuong13 = GetInt32Attribute(ele, "SanLuong13");
                    if (GetStringAttribute(ele, "SanLuong14") != "") inf.SanLuong14 = GetInt32Attribute(ele, "SanLuong14");
                    if (GetStringAttribute(ele, "SanLuong15") != "") inf.SanLuong15 = GetInt32Attribute(ele, "SanLuong15");
                    if (GetStringAttribute(ele, "SanLuong16") != "") inf.SanLuong16 = GetInt32Attribute(ele, "SanLuong16");
                    if (GetStringAttribute(ele, "SanLuong17") != "") inf.SanLuong17 = GetInt32Attribute(ele, "SanLuong17");
                    if (GetStringAttribute(ele, "SanLuong18") != "") inf.SanLuong18 = GetInt32Attribute(ele, "SanLuong18");
                    if (GetStringAttribute(ele, "SanLuong19") != "") inf.SanLuong19 = GetInt32Attribute(ele, "SanLuong19");
                    if (GetStringAttribute(ele, "SanLuong20") != "") inf.SanLuong20 = GetInt32Attribute(ele, "SanLuong20");
                    // details
                    //string details = GetStringAttribute(ele, "details");
                    //inf.details = JsonConvert.DeserializeObject<CsDataAccessGroupInfo.RoleDetail[]>(details);
                    lRes[i] = inf;
                }
                catch (Exception ex) { sErr = ex.Message; }
            }
            return lRes;
        }
        public bool validate(JObject o, bool pkOnly, ref string sErr)
        {
            bool inputErr = false;
            if (pkOnly)
            {
                if (o["LineId"] == null) { sErr += "Invalid LineId. Please check again"; inputErr = true; }
            }
            else
            {
                if (o["LineId"] == null) { sErr += "Invalid LineId. Please check again"; inputErr = true; }
                if (o["CongNhan1"] != null) { }//Check validate 
                if (o["CongNhan2"] != null) { }//Check validate 
                if (o["CongNhan3"] != null) { }//Check validate 
                if (o["CongNhan4"] != null) { }//Check validate 
                if (o["CongNhan5"] != null) { }//Check validate 
                if (o["CongNhan6"] != null) { }//Check validate 
                if (o["CongNhan7"] != null) { }//Check validate 
                if (o["SanLuong1"] != null) { }//Check validate 
                if (o["SanLuong2"] != null) { }//Check validate 
                if (o["SanLuong3"] != null) { }//Check validate 
                if (o["SanLuong4"] != null) { }//Check validate 
                if (o["SanLuong5"] != null) { }//Check validate 
                if (o["SanLuong6"] != null) { }//Check validate 
                if (o["SanLuong7"] != null) { }//Check validate 
                if (o["SanLuong8"] != null) { }//Check validate 
                if (o["SanLuong9"] != null) { }//Check validate 
                if (o["SanLuong10"] != null) { }//Check validate 
                if (o["SanLuong11"] != null) { }//Check validate 
                if (o["SanLuong12"] != null) { }//Check validate 
                if (o["SanLuong13"] != null) { }//Check validate 
                if (o["SanLuong14"] != null) { }//Check validate 
                if (o["SanLuong15"] != null) { }//Check validate 
                if (o["SanLuong16"] != null) { }//Check validate 
                if (o["SanLuong17"] != null) { }//Check validate 
                if (o["SanLuong18"] != null) { }//Check validate 
                if (o["SanLuong19"] != null) { }//Check validate 
                if (o["SanLuong20"] != null) { }//Check validate 
            }
            return inputErr;
        }
        public override bool isField(string field)
        {
            return new DTO.ChuyenLCDInfo().isField(field);
        }
        #endregion Method



        public ChuyenLCDInfo[] GetChuyenLCD(string XuongId, int line, ref string logMsg)
        {
            return _objDAO.GetChuyenLCD(XuongId, line, ref logMsg);
        }

    }
}
